package com.yuwei.module_visit

import android.app.Activity
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import com.yuwei.http.entity.AddVisitEntity
import com.yuwei.module_visit.databinding.ActivityAddVisitBinding
import com.yuwei.module_visit.viewmodel.VisitViewModel
import java.text.SimpleDateFormat


@Route(path = RouterPath.MODULE_ADD_VISIT_ACTIVITY)
class AddVisitActivity : BaseMvvmActivity<ActivityAddVisitBinding>() {
    val listOf = mutableListOf<String>()
    val vm : VisitViewModel by viewModels()
    override fun initViews() {
        binding.addVisitCarNum.isEnabled = false
        binding.addVisitDataView.setOnClickListener {
            DatePickerDialog(this,
                { _, year, month, dayOfMonth ->
                    binding.addVisitData.text = "$year-${month+1}-$dayOfMonth"
                }, 2021, 1, 1).show()
        }
        binding.addVisitStartTime.setOnClickListener {
            TimePickerDialog(this,
                { _, hourOfDay, minute ->
                    binding.addVisitStartTime.text = "$hourOfDay:$minute"
                }, 10, 10, true).show()
        }
        binding.addVisitEndTime.setOnClickListener {
            TimePickerDialog(this,
                { _, hourOfDay, minute ->
                    binding.addVisitEndTime.text = "$hourOfDay:$minute"
                }, 10, 10, true).show()
        }
        binding.visitBack.setOnClickListener {
            finish()
        }
        binding.addVisitParkingSwitch.setOnCheckedChangeListener { _, isChecked ->
            binding.addVisitCarNum.isEnabled = isChecked
        }
        val regex = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][·]?[A-HJ-NP-Z0-9]{4,5}([A-HJ-NP-Z0-9挂学警港澳])?\$"
        binding.addVisitSave.setOnClickListener{

            if  (binding.addVisitParkingSwitch.isChecked) {
                if (binding.addVisitCarNum.text.toString().isEmpty()) {
                    binding.addVisitCarNum.error = "请填写车牌号"
                    ToastUtils.showLong("请输入车牌号")
                    return@setOnClickListener
                }
                if(!binding.addVisitCarNum.text.toString().matches(regex.toRegex())){
                    if (binding.addVisitCarNum.text.toString().length < 7) {
                        binding.addVisitCarNum.error = "车牌号格式不正确"
                        ToastUtils.showLong("车牌号格式不正确")
                        return@setOnClickListener
                    }
                }
            }
            if (binding.addVisitPurpose.text.toString().isEmpty()) {
                binding.addVisitPurpose.error = "请填写访问事由"
                ToastUtils.showLong("请输入访问事由")
                return@setOnClickListener
            }
            if (binding.addVisitName.text.toString().isEmpty()) {
                binding.addVisitName.error = "请填写访客姓名"
                ToastUtils.showLong("请输入访客姓名")
                return@setOnClickListener
            }
            if (binding.addVisitPhoneNum.text.toString().isEmpty()) {
                binding.addVisitPhoneNum.error = "请填写访客手机号"
                ToastUtils.showLong("请输入访客手机号")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileExact(binding.addVisitPhoneNum.text.toString())){
                binding.addVisitPhoneNum.error = "请填写正确的手机号"
                ToastUtils.showLong("请输入正确的手机号")
                return@setOnClickListener
            }
            if (binding.addVisitCardNum.text.toString().isEmpty()) {
                binding.addVisitCardNum.error = "请填写访客身份证号"
                ToastUtils.showLong("请输入访客身份证号")
                return@setOnClickListener
            }
            if(!RegexUtils.isIDCard18(binding.addVisitCardNum.text.toString())){
                binding.addVisitCardNum.error = "请填写正确的身份证号"
                ToastUtils.showLong("请输入正确的身份证号")
                return@setOnClickListener
            }
            if (binding.addVisitEmployee.text.toString().isEmpty()){
                binding.addVisitEmployee.error = "请填写访问对象"
                ToastUtils.showLong("请输入访问对象")
                return@setOnClickListener
            }

            val param = AddVisitEntity(
                appointmentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()),
                requestParking = binding.addVisitParkingSwitch.isChecked,
                employeeId = 1,
                idNumber = binding.addVisitCardNum.text.toString().toLong(),
                plateNumber = binding.addVisitCarNum.text.toString(),
                visitStatus = "待审核",
                visitorName = binding.addVisitName.text.toString(),
                id = 0,
                purpose = binding.addVisitPurpose.text.toString(),
                attachments = listOf,
                approved = false,
                phoneNumber = binding.addVisitPhoneNum.text.toString().toLong(),
            )
            vm.addVisit(param)
        }
        binding.addVisitLoadFile.setOnClickListener {
            PictureSelector.create(this, 512).selectPicture()
        }
    }
    override fun observeDataChange() {
        vm.addVisitSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_add_visit
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==512&&resultCode== Activity.RESULT_OK){
            val extras = data?.extras
            val any = extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            listOf.add(any.path)
            Glide.with(this).load(any.path).into(binding.addVisitImage)
            if(listOf.size>0){
                binding.addVisitLoadFile.visibility = View.GONE
            }
        }

    }
}